package com.android.Translator.View;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.android.Translator.R;
import com.android.Translator.Presenter.TranslatorPresenter;
import com.android.Translator.View.MyAdapter.ListExampleAdapter;
import com.android.Translator.View.MyAdapter.ListMeanAdapter;
import com.example.entity.DataSearch;


public class TranslatorActivity extends Activity implements OnClickListener{
	/** Called when the activity is first created. */
	private TranslatorPresenter m_TranslatorPresenter;
	private EditText input;
	private Button btnTranslate;
	private ListView m_ListView;
	private ListView m_ListExamples;
	private ListMeanAdapter m_MeanAdapter;
	private ListExampleAdapter m_ExampleAdapter;
	private TabHost tabs;   
	private LinearLayout tab1, tab2;
	private Button m_BtnLearn, m_BtnAddWord;
	
	private OnTouchListener myOnTouchListener = new OnTouchListener() {
		
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			// TODO Auto-generated method stub
			int id = v.getId();
			switch (id)
			{
			//nhanvt edit start
			case R.id.tab1:
				//nhanvt edit start
				m_ListView.clearTextFilter();
				m_ListExamples.clearTextFilter();
				m_ListView.setAdapter(m_MeanAdapter);
				break;
			case R.id.tab2:
				m_ListView.clearTextFilter();
				m_ListExamples.clearTextFilter();
				m_ListExamples.setAdapter(m_ExampleAdapter);
				break;
			}
			//nhanvt edit end
			return false;
		}
	};
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /*
        //[Quan] cho nay de fix bug crash no cho phep network co the access UI thread
         */
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy); 
        //[Quan] end
        
       
        setContentView(R.layout.translate);
        InitView();
        //nhanvt start
        
        //init TranslatorPresenter
        m_TranslatorPresenter = new TranslatorPresenter(this);
        //end
        //nhanvt end
    }
    @Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		
		Intent i = getIntent();
		if (i != null)
		{
			Bundle bundel = i.getParcelableExtra("BUNDLE");
			String KeyWord=null;
			if (bundel!= null)
			{
				KeyWord = bundel.getString("KEY_WORD");
			    input.setText(KeyWord);
			    TranslatorPresenter.Translate(this, KeyWord);
			    tabs.setVisibility(View.VISIBLE);
			}
		}
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}
    private void InitView()
    {
    	input = (EditText) findViewById(R.id.inputText);
    	btnTranslate = (Button) findViewById(R.id.btnTranslate);
    	btnTranslate.setOnClickListener(this);
    	m_BtnAddWord = (Button) findViewById(R.id.btnAddWord);
    	m_BtnAddWord.setOnClickListener(this);
    	
    	
    	m_ListView = (ListView) findViewById(R.id.listResult);
    	m_ListExamples = (ListView) findViewById(R.id.listExample);
    	tabs = (TabHost)findViewById(R.id.tabHost);
		tabs.setVisibility(View.INVISIBLE);

        tabs.setup();

        tab1 = (LinearLayout) findViewById(R.id.tab1);
        //[Quan] -- Set Touch Listener cho tab1
        tab1.setOnTouchListener(myOnTouchListener);
        tab2 = (LinearLayout) findViewById(R.id.tab2);
        //[Quan] -- Set Touch Listener cho tab2
        tab2.setOnTouchListener(myOnTouchListener);
        
        setupTab(tab1, getString(R.string.TAB_WORDS));
        setupTab(tab2, getString(R.string.TAB_EXAMPLES));
        
        
      /*  DisplayMetrics displaymetrics = new DisplayMetrics();
        int width = displaymetrics.widthPixels;
        int btnWidth = (int) (width/1.6);
    	m_BtnAddWord = (Button) findViewById(R.id.MM_btnAddWord);
		m_BtnAddWord.setOnClickListener(this);
		m_BtnAddWord.setWidth(btnWidth);*/
       
    }   
    private void setupTab(final View view, final String tag) {
	    View tabview = createTabView(tabs.getContext(), tag);
	    TabSpec setContent = tabs.newTabSpec(tag).setIndicator(tabview).setContent(new TabHost.TabContentFactory() {
	        public View createTabContent(String tag) {
	            return view;
	        }
	    });
	    tabs.addTab(setContent);
	}

	private static View createTabView(final Context context, final String text) {
	    View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
	    TextView tv = (TextView) view.findViewById(R.id.tabsText);
	    tv.setText(text);
	    return view;
	}
   //[Quan]ham nay dung de lay chuoi can dich
    public String getInputText()
    {
    	return input.getText().toString().toLowerCase();
    }
    //end
   
    //nhanvt start
    public void setAdapterForListLocalView(DataSearch result)
    {
    	m_MeanAdapter = new ListMeanAdapter(this, result.getLstMean(), R.drawable.icon_check);
		m_ListView.setAdapter(m_MeanAdapter);
    	m_ExampleAdapter = new ListExampleAdapter(this, result.getLstExam());
    }
    //nhanvt end

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int id = v.getId();
		
		InputMethodManager imm = (InputMethodManager)getSystemService(
			      Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
		
		switch(id)
		{
		/*[Quan] su li su kien click cac button
		co the su li them nhieu button khac nua
		voi moi button ta se them 1 case vao*/
		
		case R.id.btnTranslate:
			// get Internet status
			//[Quan]clear listview content
			if (m_MeanAdapter != null && m_ExampleAdapter != null)
			{
				m_MeanAdapter.clear();
				m_MeanAdapter.notifyDataSetChanged();
				m_ExampleAdapter.clear();
				m_ExampleAdapter.notifyDataSetChanged();
			}
			tabs.setVisibility(View.VISIBLE);
			//end clear listview content
			TranslatorPresenter.Translate(this, getInputText());
			break;
		case R.id.btnAddWord:
			Intent i;
			i = new Intent(getApplicationContext(), AddWordToLearn.class);
			startActivity(i);
			break;
		}
		

		
	}
	
}